//
//  WHYPictureViewController.m
//  News163
//
//  Created by 王洪运 on 16/1/8.
//  Copyright © 2016年 王洪运. All rights reserved.
//

#import "WHYPictureViewController.h"

@interface WHYPictureViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation WHYPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 按钮点击事件
- (IBAction)clickReturnBarButtonItem:(UIBarButtonItem *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"pictureCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    
    cell.textLabel.text = @"图片cell";
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    cell.backgroundColor = [UIColor randomColor];
    
    return cell;
}

#pragma mark - UITableViewDelegate

#pragma mark - UIScrollViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    self.iconImageView.animationRepeatCount = 1;
    self.iconImageView.animationDuration = self.iconImageView.animationImages.count * 0.2;
    [self.iconImageView startAnimating];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.tableView.transform = CGAffineTransformMakeTranslation(0, 80);
    }];
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [UIView animateWithDuration:1.0 animations:^{
        self.tableView.transform = CGAffineTransformMakeTranslation(0, 0);
    }];
    
}

#pragma mark - 懒加载图片
-(UIImageView *)iconImageView {
    if (_iconImageView.animationImages == nil) {
        NSMutableArray *imageArr = [NSMutableArray array];
        
        for (int i = 1; i < 35; i++) {
            
            NSString *imageName = [NSString stringWithFormat:@"bobo_flower_frame%02d.png",i];
            
            UIImage *image = [UIImage imageNamed:imageName];
            
            [imageArr addObject:image];
            
        }
        
        _iconImageView.animationImages = imageArr;
        
    }
    return _iconImageView;
}


@end
